package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.VideoManager;
import com.itheima.domain.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class VideoController {

    @Autowired
    VideoManager videoManager;

    @GetMapping("/smallVideos")
    public ResponseEntity findRecommendVideoByPage(
            @RequestParam(defaultValue = "1",value = "page") int pageNum,
            @RequestParam(defaultValue = "8",value = "pagesize") int pageSize){
        //1.获取当前用户id
        Long userId = UserHolder.get().getId();

        //2.调用manager完成分页操作,返回pageBeanVo
        PageBeanVo page = videoManager.findRecommendVideoByPage(pageNum,pageSize,userId);

        //3.返回ResponseEntity
        return ResponseEntity.ok(page);
    }

    @PostMapping("/smallVideos")
    public ResponseEntity save(MultipartFile videoThumbnail,MultipartFile videoFile) throws IOException {
        //1.接受参数
        //2.获取当前用户的id
        Long userId = UserHolder.get().getId();

        //3.调用manager完成保存
        videoManager.save(videoThumbnail,videoFile,userId);

        //4.返回ResponseEntity
        return ResponseEntity.ok(null);
    }

    @PostMapping("/smallVideos/{focusUserId}/userFocus")
    public ResponseEntity focusUser(@PathVariable Long focusUserId){
        return videoManager.focusUser(focusUserId);
    }

    @PostMapping("/smallVideos/{focusUserId}/userUnFocus")
    public ResponseEntity UnFocusUser(@PathVariable Long focusUserId){
        return videoManager.unFocusUser(focusUserId);
    }
}
